// JavaScript Document
var xmlhttp;
function GetXmlHttpObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
} 

/*--------------------Phat-------------------*/
//---------------Dang Nhap----------------
function KiemTraDangNhap()
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null )
	{
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	var n1 = document.getElementById('username').value;
	var n2 = document.getElementById('password').value;
	var n3 = document.getElementById('remember').checked;

	if( n1 == "" || n2 == "" )
	{
		document.location.href = "index.php?com=user&fun=login&Error=1";
	}
	else
	{
		var url ="index.php?com=user&fun=login&Error=0&user=" + n1 + "&pass=" + n2 + "&remember=" + n3;
		url += "&t="+ Math.random();
		document.location.href = url;
	}
}
//-------------------Dang ky---------------------//
function ktuser()
{
	var tendangnhap = document.getElementById("addTenDangNhap").value;
	var loiuser = document.getElementById("loitendangnhap");
	loiuser.style.color = "red";
	if (tendangnhap == "")
	{
		loiuser.innerHTML = "<img src=images/NotOk.gif>";//&nbsp;Không bỏ trống trường bắt buột"  ;
		return false;
	}
	if (tendangnhap.length < 4 )
	{
		loiuser.innerHTML = "<img src=images/NotOk.gif>";//&nbsp;Tên đăng nhập phải lớn hơn 4 kí tự"  ;
		return false;
	}
	var flag =0 ;
	for (var i=0 ; i < tendangnhap.length ; i++)
		if(( tendangnhap[i] < '0' || tendangnhap[i] > '9') && (tendangnhap[i] >'z' || tendangnhap[i] <'a') && (tendangnhap[i] >'Z' || tendangnhap[i] < 'A') )
			flag = 1;
	if(flag ==1)
	{
		loiuser.innerHTML = "<img src=images/NotOk.gif>";//&nbsp;Tên đăng nhập phải gồm kí tự a->z,0->9."  ;
		return false;
	}
	loiuser.innerHTML = "<img src=images/Ok.gif"  ;
	return true;
}

function ktmatkhau1()
{
	var matkhau1 =    document.getElementById("addMatKhau").value;
	var loi = document.getElementById("loimatkhau");
	loi.style.color = "red";
	if(  matkhau1 == "" )
	{
		//loi.innerHTML = "Không bỏ trống trường bắt buột"  ;
		loi.innerHTML = "<img src=images/NotOk.gif>"  ;
		return ;
	}
	if(  matkhau1.length < 6 )
	{
		//loi.innerHTML = "Mật khẩu phải nhiều hơn 6 kí tự" ;
		loi.innerHTML = "<img src=images/NotOk.gif>" ;
		return ;
	}
	//loi.innerHTML = ""  ;
	loi.innerHTML = "<img src=images/Ok.gif>"  ;
}

function ktmatkhau2()
{
	var matkhau1 = document.getElementById("addMatKhau").value;
	var matkhau2 = document.getElementById("addReMatKhau").value;
	var loi = document.getElementById("loairematkhau");
	loi.style.color = "red";
	if(matkhau1 != matkhau2 )
	{
		//loi.innerHTML ="Mật khẩu chưa khớp";
		loi.innerHTML = "<img src=images/NotOk.gif>"  ;
		return ;
	}
	//loi.innerHTML = ""  ;
	loi.innerHTML = "<img src=images/Ok.gif>"  ;
}

function checkEmail(email) 
{
	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) 
		return 0;
	return 1;
}

function ktemailhople()
{
	var email1 =document.getElementById("addEmail").value;
	var loi = document.getElementById("loiemail");
	loi.style.color = "red";
	if(  email1 == "" )
	{
		//loi.innerHTML = "Không bỏ trống trường bắt buột" ;
		loi.innerHTML = "<img src=images/NotOk.gif>"  ;
		return ;
	}
	if( checkEmail(email1) == 0 )
	{
		//loi.innerHTML = "Email không hợp lệ" ;
		loi.innerHTML = "<img src=images/NotOk.gif>"  ;
		return ;
	}
	//loi.innerHTML = ""  ;
	loi.innerHTML = "<img src=images/Ok.gif>"  ;
}

function ktkhopemail()
{
	var email1 =document.getElementById("addEmail").value;
	var email2 =document.getElementById("addReEmail").value;
	var loi = document.getElementById("loireemail");
	loi.style.color = "red";
	if(  email1 != email2 )
	{
		//loi.innerHTML = "Email chưa khớp" ;
		loi.innerHTML = "<img src=images/NotOk.gif>"  ;
		return ;
	}
	//loi.innerHTML = ""  ;
	loi.innerHTML = "<img src=images/Ok.gif>"  ;
}

function kthoten()
{
	var hoten =document.getElementById("addHoTen").value;
	var loi = document.getElementById("loihoten");
	loi.style.color = "red";
	if ( hoten == "" )
	{
		//loi.innerHTML = "Không bỏ trống trường bắt buột" ;
		loi.innerHTML = "<img src=images/NotOk.gif>"  ;
		return ;
	}
//	loi.innerHTML = ""  ;
	loi.innerHTML = "<img src=images/Ok.gif>"  ;
}

function ktdienthoai()
{
	var dienthoai =document.getElementById("addDienThoai").value;
	var loi = document.getElementById("loidienthoai");
	loi.style.color = "red";
	if(isNaN(dienthoai))
	{
		//loi.innerHTML ="Điện thoại phải nhập số" ;
		loi.innerHTML = "<img src=images/NotOk.gif>"  ;
		return;
	}
	if(dienthoai.length>10)
	{
		//loi.innerHTML ="Điện thoại phải nhập số" ;
		loi.innerHTML = "<img src=images/NotOk.gif>"  ;
		return;
	}
	loi.innerHTML = ""  ;
}

function KiemTraCode()
{
	var kq = document.getElementById("ErrorCode");
	var n1 = document.getElementById('security_code').value;
	if( n1 == "" )
	{
		kq.style.color = "red";
		kq.innerHTML = "<img src=images/NotOk.gif>";
		return false;
	}
	else
	{
		kq.innerHTML = "";
		return true;
	}
}

function kttendangnhap(tendangnhap)
{
	if (tendangnhap.length <6 )
		return false ;
	for (var i=0 ; i < tendangnhap.length ; i++)
		if(( tendangnhap[i] < '0' || tendangnhap[i] > '9') && (tendangnhap[i] >'z' || tendangnhap[i] <'a') && (tendangnhap[i] >'Z' || tendangnhap[i] < 'A') )
			return false ;
	return true ;
}

function clearloi()
{
	document.getElementById("loitendangnhap").innerHTML = "" ;
	document.getElementById("loimatkhau").innerHTML = "" ;
	document.getElementById("loairematkhau").innerHTML ="";
	document.getElementById("loiemail").innerHTML = "" ;
	document.getElementById("loireemail").innerHTML ="";
	document.getElementById("loihoten").innerHTML ="";
	document.getElementById("loidienthoai").innerHTML ="";
}

function kiemtradangki()
{
	xmlHttp = GetXmlHttpObject();
	if (xmlHttp == null )
	{
		alert ("Your browser does not support XML HTTP Request");
		return;
	}
	clearloi() ;
	
	var ch = document.getElementsByName("checkdangky[]");	
	var IDsLinhvuc = "" ;
	
	for (var i=0 ; i < ch.length ; i++ )
	{
		if ( ch[i].checked == true )
		{
			if( IDsLinhvuc == "" )
				IDsLinhvuc = ch[i].value ;
			else
				IDsLinhvuc += ","+ ch[i].value ;
		}
	}
	
	var tendangnhap =  encodeURI(document.getElementById("addTenDangNhap").value);
	var matkhau1 =  encodeURI(document.getElementById("addMatKhau").value);
	var matkhau2 = encodeURI( document.getElementById("addReMatKhau").value);
	var email1 =  encodeURI(document.getElementById("addEmail").value);
	var email2 =  encodeURI(document.getElementById("addReEmail").value);
	var hoten =  encodeURI(document.getElementById("addHoTen").value);
	var diachi =  encodeURI(document.getElementById("addDiaChi").value);
	var dienthoai = document.getElementById("addDienThoai").value;
	var checkgioitinh = document.getElementById("addGioiTinh").checked;
	
	if(checkgioitinh == true)
		var gioitinh = 1;
	else
		var gioitinh = 0;
	
	var ngay = document.getElementById("ngay").value;
	var thang = document.getElementById("thang").value;
	var nam = document.getElementById("nam").value;
	var ngaysinh = nam +"-"+ thang +"-"+ngay ;
	
	var hinhanh = encodeURI(document.getElementById("addHinhAnh").value);
	if ( hinhanh == "" )
		hinhanh = 'nopic1.gif';
		
	var code = document.getElementById('security_code').value;
	
	var kt = true ;
	kt = ktuser();
	if (ktuser()==true)
		var loiuser = document.getElementById("loitendangnhap").innerHTML = "";

	if(  matkhau1=="" )
	{
		document.getElementById("loimatkhau").innerHTML = "<img src=images/NotOk.gif>";
		kt =false ;
	}else if(  matkhau1.length < 6 )
	{
		document.getElementById("loimatkhau").innerHTML = "<img src=images/NotOk.gif>";
		kt =false ;
	}
	if(matkhau1 != matkhau2 )
	{
		document.getElementById("loairematkhau").innerHTML = "<img src=images/NotOk.gif>";
		kt =false ;
	}
	if(  email1 == "" )
	{
		document.getElementById("loiemail").innerHTML = "<img src=images/NotOk.gif>";
		kt =false ;
	}
	else if(checkEmail(email1) == 0)
	{
		document.getElementById("loiemail").innerHTML = "<img src=images/NotOk.gif>";
		kt =false ;
	}
	if(email1 != email2 )
	{
		document.getElementById("loireemail").innerHTML = "<img src=images/NotOk.gif>";
		kt =false ;
	}
	if ( hoten == "" )
	{
		document.getElementById("loihoten").innerHTML = "<img src=images/NotOk.gif>";
		kt =false ;
	}

	if( code == "" )
	{	
		var kq = document.getElementById("ErrorCode");
		kq.innerHTML = "<img src=images/NotOk.gif>";
		kt =  false;
	}
	
	if(kt == false)
		return false;
	
	var url ="index.php?com=user&fun=register&tendangnhap="+tendangnhap;
				url += "&email="+email1 ;
				url += "&matkhau="+matkhau1 ;
				url += "&hoten="+hoten ;
				url += "&ngay="+ngay;
				url += "&thang="+thang;
				url += "&nam="+nam;
				url += "&diachi="+diachi ;
				url += "&dienthoai="+dienthoai ;
				url += "&gioitinh="+gioitinh ;
				url += "&hinhanh="+hinhanh ;
				url += "&code="+code ;IDsLinhvuc
				url += "&IDsKhoa="+IDsLinhvuc ;
				url += "&Type=1";
				url += "&t="+ Math.random();
	document.location.href = url;
}

//---------update---------/

function XuLyCapNhatCSDLUser(ID)
{
	var hotenCu = encodeURI(document.getElementById("HoTenCu").value);
	var hotenMoi = encodeURI(document.getElementById("upHoTen").value);
	
	var index1 = document.getElementById("ngay").value;
	var index2 = document.getElementById("thang").value;
	var index3 = document.getElementById("nam").value;
	var ngaysinh = index3 +"-"+ index2 +"-"+index1 ;
	
	var diachiCu = encodeURI(document.getElementById("DiaChiCu").value);
	var diachiMoi = encodeURI(document.getElementById("upDiaChi").value);
	
	var dienthoaiCu = encodeURI(document.getElementById("DienThoaiCu").value);
	var dienthoaiMoi = encodeURI(document.getElementById("upDienThoai").value);
	
	
	var checkgioitinh = document.getElementById("upGioiTinh").checked;
	
	var hinhanhCu = encodeURI(document.getElementById("HinhAnhCu").value);
	var hinhanhMoi = encodeURI(document.getElementById("upHinhAnh").value);
	
	var kt = true ;
	
	if ( hotenMoi == "" )
		var hoten = hotenCu;
	else
		var hoten = hotenMoi;
				
	if (diachiMoi  == "" )
		var diachi = diachiCu;
	else
		var diachi = diachiMoi;
	
	if ( dienthoaiMoi == "" )
	{
		var dienthoai = dienthoaiCu;
		document.getElementById("loidienthoai").innerHTML ="" ;
	}
	else if(isNaN(dienthoaiMoi))
	{
		document.getElementById("loidienthoai").style.color = "red";
		document.getElementById("loidienthoai").innerHTML ="Không được nhập chữ" ;
		kt =false ;
	}else{
		var dienthoai = dienthoaiMoi;
		document.getElementById("loidienthoai").innerHTML ="" ;
	}
		
	if ( hinhanhMoi == "" )
	{
	//	alert (hinhanhCu);
		var hinhanh = hinhanhCu;
		var doihinh = 0;
	}
	else 
	{
		var hinhanh = hinhanhMoi;
		var doihinh = 1;
	}
			
	if(checkgioitinh == true)
		var gioitinh = 1;
	else
		var gioitinh = 0;	

	if(kt == true)
	{
		var url ="index.php?com=user&fun=updatethongtin&mauser="+ID;
			url += "&hoten="+hoten ;
			url += "&ngaysinh="+ngaysinh;
			url += "&diachi="+diachi ;
			url += "&dienthoai="+dienthoai ;
			url += "&gioitinh="+gioitinh ;
			url += "&hinhanh="+hinhanh ;
			url += "&doihinh="+doihinh ;
			url += "&Type=1";
			url += "&t="+ Math.random();

		document.location.href = url;
	}
	
}

function UpdateTaiKhoanUser(ID)
{
	var pass = encodeURI(document.getElementById("upPass").value);
	var passnew = encodeURI(document.getElementById("upPassNew").value);
	var repassnew = document.getElementById("upRePassNew").value;
	var code = document.getElementById("security_code").value;
	
	var loi = document.getElementById("loiupdate");
	loi.style.color = "red";
	var kt = true ;
	
	if ( pass == "" )
	{
		loi.innerHTML = "&nbsp;Chưa Nhập Mật khẩu củ"  ;
		return false;
	}
				
	if (passnew  == "" )
	{
		loi.innerHTML = "&nbsp;Chưa Nhập Mật khẩu mới"  ;
		return false;
	}
	if (passnew.length < 4 )
	{
		loi.innerHTML = "&nbsp;Mật khẩu phải nhiều hơn 4 ký tự"  ;
		return false;
	}
	
	if (repassnew == "" )
	{
		loi.innerHTML = "&nbsp;Chưa Nhập xác nhận Mật khẩu mới"  ;
		return false;
	}
	if(passnew != repassnew)
	{
		loi.innerHTML = "&nbsp;Xác Nhận Mật Khẩu không khóp"  ;
		return false;
	}
	
	
	var url ="index.php?com=user&fun=updatepass&ma="+ID;
		url += "&pass="+pass ;
		url += "&passnew="+passnew;
		url += "&code="+code ;
		url += "&Type=1";

	document.location.href = url;
	
}

//-------------- quen pass ------------------------
function kiemtralaypassemail()
{
	var email =  encodeURI(document.getElementById("passemail").value);
	if(  email == "" )
	{
		document.getElementById("loiemail").innerHTML = "Chưa nhập mail !!!";
		return false ;
	}
	if(checkEmail(email) == 0)
	{
		document.getElementById("loiemail").innerHTML = "Email không đúng định dạng!!!";
		return false ;
	}
	
	var url ="index.php?com=user&fun=quenpass";
				url += "&email="+email;
				url += "&Type=1";
	document.location.href = url;
}
/*--------------------End-Phat-------------------*/

function showResult()
{
	if (xmlhttp.readyState==4 && xmlhttp.status == 200)
	{
		document.getElementById("entry").innerHTML=xmlhttp.responseText;
	}
}
